package week6.day40.title30;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 1.创建一个学生数组，学生里面有 id name address age 这些属性。
 * 创建5个学生放入。 要求如下：
 * 1.统计学生有多少是湖北的。
 * 2.将年龄大于18的同学找出来。不能光打印，需要存储下来，
 * 3.统计一下名字中包含 z 的且年龄大于18的,存储下来。
 * @Author fengzihao
 * @Date 2024-05-24 19:49
 */
public class StudentTest {
    public static void main(String[] args) {
        // 生成学生列表
        ArrayList<Student> students = generateStudentList();
        // 计算出湖北学生人数
        long hubeiCount = students.stream().filter(student -> "hubei".equals(student.address)).count();
        System.out.println("hubei的学生人数 = " + hubeiCount);
        // 找出并存储打印大于18的学生
        List<Student> collect = students.stream().filter(student -> student.age > 18).collect(Collectors.toList());
        for (Student student : collect) {
            System.out.println("student age more than 18 = " + student);
        }
        // 找出名字中含有z并且年龄大于18的同学
        List<Student> collect1 = students.stream().filter(student -> student.name.contains("z")).filter(student -> student.age > 18).collect(Collectors.toList());
        for (Student student : collect1) {
            System.out.println("student age more than 18 and name contains z  = " + student);
        }
    }

    private static ArrayList<Student> generateStudentList() {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student(1,"zs","hubei",22));
        students.add(new Student(2,"ls","henan",19));
        students.add(new Student(3,"wu","beijing",18));
        students.add(new Student(4,"zl","hubei",17));
        students.add(new Student(5,"fzh","hubei",19));
        return students;
    }
}
class Student implements Comparable<Student>{
    int id;
    String name;
    String address;
    int age;

    public Student() {
    }

    public Student(int id, String name, String address, int age) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id && age == student.age && Objects.equals(name, student.name) && Objects.equals(address, student.address);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, address, age);
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age-o.age;
    }
}

